package com.gz.mylife;

import java.io.File;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;






import org.xutils.x;

import android.app.Application;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.util.SparseArray;

import com.gz.mylife.app.Global;
import com.gz.mylife.po.ChoosePhotoInfo;
import com.gz.mylife.util.ObserverWorker;
import com.nostra13.universalimageloader.cache.disc.impl.LimitedAgeDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.decode.BaseImageDecoder;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.utils.StorageUtils;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.umeng.socialize.Config;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;
import com.zl.smartmall.library.ISmartMall;
import com.zl.smartmall.library.po.DeviceBean;
import com.zl.smartmall.library.util.Constants;
import com.zl.smartmall.library.util.CrashHandler;

public class App extends Application {

	public static Boolean showlog=true;
	public static String img_name = "";
	public static String time = "";//当前时间，为维修上报拍照图片命名使用
	public static SparseArray<ChoosePhotoInfo> mChoosePhotoInfoQueue;
	public static boolean mTimeState = true;
	public static Map<String, Long> mTimeMap;
	private static Context context;
	private static Handler handler;
	public static String appip="";
	public static List<DeviceBean> devicelist=new ArrayList<DeviceBean>();
	public static String AppUrl=Constants.URL_HOST_DOOR;
	public static String sendtype="";
	public static DatagramSocket ds;
	//为了看出这次点击的是寻找方法还是open方法
	public static String type="";
	//点击的这个item、的ip
	public static String tagip="";
	// 一个app的入口

	@Override
	public void onCreate() {
		super.onCreate();


		//-----------------
		//开启debug模式，方便定位错误，具体错误检查方式可以查看http://dev.umeng.com/social/android/quick-integration的报错必看，正式发布，请关闭该模式
		Config.DEBUG = true;
		//QueuedWork.isUseThreadPool = false;
		UMShareAPI.get(this);
		//-----------------------------------

		x.Ext.init(this);
		x.Ext.setDebug(BuildConfig.DEBUG);
		CrashHandler.init(this);
		Global.init(this);
		ISmartMall.init(this);
		ObserverWorker.init();
		//		String s = null;
		//		s.toCharArray();


		// UnlimitedDiscCache 不限制缓存大小
		// TotalSizeLimitedDiscCache 对缓存大小限制。如果缓存大小超过规定限额然后文件最老的最后使用日期的将被删除
		// FileCountLimitedDiscCache 对文件数量限制，对最近最少使用的的删除
		// LimitedAgeDiscCache 限定一个日期，过期的将被删除

		DisplayImageOptions options = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.ic_load_image_square_default)
		.showImageOnFail(R.drawable.ic_load_image_square_fail)
		.showImageForEmptyUri(R.drawable.ic_load_image_square_fail)
		.resetViewBeforeLoading(true)
		.bitmapConfig(Bitmap.Config.ARGB_8888)
		.cacheInMemory(true)
		.cacheOnDisk(true)
		.imageScaleType(ImageScaleType.EXACTLY) 
		.build();
		File cacheDir = StorageUtils.getCacheDirectory(getApplicationContext());
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
		//.memoryCacheExtraOptions(480, 800) // default = device screen dimensions
		//        	.discCacheExtraOptions(getResources().getDisplayMetrics().widthPixels,
		//        			getResources().getDisplayMetrics().heightPixels, CompressFormat.PNG, 100)
		.threadPoolSize(3) // default
		.threadPriority(Thread.NORM_PRIORITY - 1) // default
		.tasksProcessingOrder(QueueProcessingType.LIFO) // default
		.denyCacheImageMultipleSizesInMemory()
		.memoryCache(new LruMemoryCache(2 * 1024 * 1024))
		.diskCache(new LimitedAgeDiscCache(cacheDir, 3600 * 24 * 7)) // 一个星期
		.diskCacheFileNameGenerator(new Md5FileNameGenerator())       // default
		.imageDownloader(new BaseImageDownloader(getApplicationContext())) // default
		.imageDecoder(new BaseImageDecoder(true)) // default
		.defaultDisplayImageOptions(options)    // default
		.build();
		ImageLoader.getInstance().init(config);

		//启动清扫服务
		//		startService(new Intent(getApplicationContext(), CleanService.class));

		//注册微信平台 通过WXAPIFactory工厂，获取IWXAPI的实例
		IWXAPI wechatApi = WXAPIFactory.createWXAPI(this, Define.APP_ID_WECHAT, false);
		wechatApi.registerApp(Define.APP_ID_WECHAT);


		context = getApplicationContext();
		handler = new Handler();
		try {
			if(ds==null){
				ds = new DatagramSocket(null);
				ds.setReuseAddress(true);
				ds.bind(new InetSocketAddress(getrandom()));
			}
		} catch (SocketException e) {
			e.printStackTrace();
		}

	}

	//各个平台的配置，建议放在全局Application或者程序入口
	{
		//双明正规的
		//PlatformConfig.setWeixin("wx9b323f1f5bd1b74d","d7bdf065d4554cca5d160a5048efe0b3");
		  PlatformConfig.setWeixin("wxe544df8841a35c03","da0f0d44b5c2fe405e6dba8914994064");
		//PlatformConfig.setQQZone("100424468", "c7394704798a158208a74ab60104f0ba");
	}


	public static Context getContext() {
		return context;
	}

	public static Handler getHandler() {
		return handler;
	}
	public int getrandom(){
		Random r = new Random();
		int xx = r.nextInt(9999);
		while(xx<4000){
			xx = r.nextInt(9999);
		}
		return xx;
	}


}
